//
// Created by 段大志 on 16/4/23.
// Copyright (c) 2016 BMProject. All rights reserved.
//

#import "BackgroundDispalyVc.h"
#import "UIViewController+common.h"
#import "BaseTableView.h"
#import "UIColor+Extension.h"
#import "BackgroundBizDisCell.h"
#import "DataModel.h"


@interface BackgroundDispalyVc ()  <UITableViewDelegate, UITableViewDataSource> {

}
@property(strong, nonatomic)  BaseTableView *tableView;
@property(strong, nonatomic)  BlackListModel *blackListModel;
@property(strong, nonatomic)  BackViewWrapModel *backViewWrapModel;

@end

@implementation BackgroundDispalyVc {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog(@"--后台业务预览--");
    NAV_INIT(self, @"后台业务预览", @"back", @selector(backButtonClick:), @"", nil);
    self.blackListModel = self.blackListWrapModel.blackListModels[self.position];
    [self initTab];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}


- (void)initTab {
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(
            0,
            0,
            UIScreenWidth,
            UIScreenHeight - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor backgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableFooterView.backgroundColor = [UIColor backgroundColor];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.page = 1;
        DLog(@"开始请求......");
        [self requestList_blackView];
    }];
    [self.tableView addFooterRefreshTarget:self action:@selector(requestLoadMoreDataByPage)];
    [self.tableView reloadData];
    [self requestList_blackView];
}


#pragma mark  ------- UITableView UITableViewDelegate or UITableViewDataSource---------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self getCount];
}

- (int)getCount{
    return self.backViewWrapModel.backMessList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 135;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BackgroundBizDisCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BackgroundBizDisCell"];
    if (!cell)
        cell = [[[UINib nibWithNibName:@"BackgroundBizDisCell" bundle:nil] instantiateWithOwner:self options:nil] lastObject];
    cell.backgroundDispalyVc = self;
    [cell setCellInfo:self.backViewWrapModel andPosition:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


#pragma mark 底部刷新
- (void)requestLoadMoreDataByPage {
    self.page++;
    [self requestList_blackView];
}

#pragma mark 请求数据

- (void)requestList_blackView {

    [UserServices blackViewToken:[[AppDelegate appDelegate] getUserInfo].token
                              id:self.blackListModel.id
                          backId:self.blackListModel.gdId
                      showDialog:YES
                 completionBlock:^(int result, id responseObject) {
                     if (result == API_Request_Success) {
                         NSMutableArray *array = responseObject;
                         [self.tableView.footer resetNoMoreData];//重置有更多数据
                         if (array == nil || array.count == 0) {
                             //提示无数据
                             [SVProgressHUD showErrorWithStatus:@"查无数据"];
                         } else {
                             _backViewWrapModel = [[BackViewWrapModel alloc] initWithDictionary:array[0] error:nil];
                             NSMutableArray<BackMessListModel*> *list = [NSMutableArray<BackMessListModel*> array];
                             [list addObjectsFromArray:_backViewWrapModel.backMessList];
                             if (_backViewWrapModel.blackMess) {
                                 [list addObject:_backViewWrapModel.blackMess];
                             }
                             _backViewWrapModel.backMessList = list;
                         }
                         [self.tableView.footer resetNoMoreData];//重置有更多数据
                         [self.tableView.header endRefreshing];
                         [self.tableView.footer endRefreshing];
                         [self.tableView reloadData];
                         [self.tableView.footer noticeNoMoreData];//没有更多数据
                     } else {//出错时的处理
                         //提示出错
                         [SVProgressHUD showErrorWithStatus:Error_Format];
                     }
                 }];
}



@end